package com.platform.qujia.service.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.platform.qujia.dao.AppUserInfoMapper;
import com.platform.qujia.dao.EnterpriseMapper;
import com.platform.qujia.dao.StationMapper;
import com.platform.qujia.pojo.AppUserInfo;
import com.platform.qujia.pojo.Enterprise;
import com.platform.qujia.pojo.Station;
import com.platform.qujia.pojo.vo.YunHuaParamsVO;
import com.platform.qujia.service.YunHuaBizService;
import com.platform.qujia.utils.RSAUtil;
import com.platform.qujia.utils.StringUtils;
import com.platform.qujia.utils.exception.CustomException;
import com.platform.qujia.utils.yunHua.yunHuaPayUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.ast.Var;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Objects;

@Service
@Slf4j
public class YunHuaBizServiceImpl implements YunHuaBizService {
    @Resource
    private AppUserInfoMapper appUserInfoMapper;

    @Resource
    private StationMapper stationMapper;

    @Value("${qujia.yunhuaParamPublicKey}")
    private String publicKey;


    @Override
    public YunHuaParamsVO getYunHuaParams(String enterpriseId, String mobilePhone) {

        AppUserInfo yunHuaUserParams = getYunHuaUserParams(mobilePhone, enterpriseId);

        JSONObject request = new JSONObject();
        request.put("mobilePhone", yunHuaUserParams.getMobilePhone());
        request.put("openId", yunHuaUserParams.getBlogOpenid());
        request.put("centerUserId", yunHuaUserParams.getYunhuaUserId());
        String content = request.toJSONString();

        String publicMessage = getPublicMessage(content, publicKey);

        String activeProfile = SpringUtil.getActiveProfile();

        String platformId = "M" + enterpriseId;
        if("dev01".equals(activeProfile) || "test01".equals(activeProfile)){
            platformId = "M141";
        }
        log.info("平台标识【{}】", platformId);
        return YunHuaParamsVO.builder()
                .platformId(platformId)
                .params(publicMessage)
                .build();
    }

    @Override
    public String getPublicMessage(String content, String publicKey) {
        String publicMessage  = RSAUtil.encryptByPublicKeyPartition(content.getBytes(StandardCharsets.UTF_8), publicKey);
        String encodePublicMessage = URLEncoder.encode(publicMessage);
        log.info("密文-->【{}】", encodePublicMessage);
        return encodePublicMessage;
    }

    private AppUserInfo getYunHuaUserParams(String mobilePhone, String enterpriseId){
//        Station station = stationMapper.selectStationById(Integer.valueOf(stationId));

        AppUserInfo appUserInfo = new AppUserInfo();
        appUserInfo.setMobilePhone(mobilePhone);
        appUserInfo.setStationId(Integer.valueOf(enterpriseId));
        AppUserInfo appUserInfoRes = appUserInfoMapper.getAppUserInfo(appUserInfo);


        if(Objects.isNull(appUserInfoRes)){
            throw new CustomException("用户不存挂在");
        }
        String yunhuaUserId = appUserInfoRes.getYunhuaUserId();
        log.info("云化userid,yunhuaUserId【{}】", yunhuaUserId);
        if(StringUtils.isEmpty(yunhuaUserId)){
            //从云化获取id
            yunhuaUserId = yunHuaPayUtils.sendUser(appUserInfoRes.getMobilePhone());
            log.info("获取云化userid,yunhuaUserId【{}】", yunhuaUserId);
            appUserInfoRes.setYunhuaUserId(yunhuaUserId);
            //更新云化userId
            appUserInfoMapper.updateYunHuaUserId(appUserInfoRes);
        }

        return AppUserInfo.builder().yunhuaUserId(yunhuaUserId)
                .blogOpenid(appUserInfoRes.getBlogOpenid()).mobilePhone(appUserInfoRes.getMobilePhone()).build();
    }
}
